<template>
  <div>
    <h1>未审核文章管理</h1> <br>
    <el-form :inline="true" :model="searchStrategy">
      <el-form-item label="标题">
        <el-input v-model="searchStrategy.title" placeholder="请输入文章标题"></el-input>
      </el-form-item>

      <el-form-item label="作者ID">
        <el-input v-model="searchStrategy.authorId" placeholder="请输入作者ID"></el-input>
      </el-form-item>

      <el-form-item label="游戏ID">
        <el-input v-model="searchStrategy.gameId" placeholder="请输入游戏ID"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">清空</el-button>
      </el-form-item>
    </el-form>

    <br><br>

    <!-- 表格 -->
    <el-table :data="strategyList" border style="width: 100%">
      <el-table-column prop="title" label="标题" width="200" align="center"></el-table-column>
      <el-table-column prop="authorId" label="作者ID" width="100" align="center"></el-table-column>
      <el-table-column prop="gameId" label="游戏ID" width="100" align="center"></el-table-column>
      <el-table-column prop="createTime" label="提交时间" width="150" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleView(scope.row.id)">查看详情</el-button>
          <el-button size="small" type="success" @click="handleApprove(scope.row.id)">通过审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 查看详情对话框 -->
    <el-dialog v-model="detailVisible" title="文章详情">
      <div class="post-detail">
        <div class="post-header">
          <h1>{{ currentPost.title }}</h1>
          <div class="post-meta">
            <span>作者ID: {{ currentPost.authorId }}</span>
            <span>游戏ID: {{ currentPost.gameId }}</span>
            <span>提交时间: {{ formatTime(currentPost.createTime) }}</span>
          </div>
        </div>
        <div class="post-content">
          <div class="markdown-body">{{ currentPost.content }}</div>
        </div>
      </div>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
        <el-button type="success" @click="handleApprove(currentPost.id)">通过审核</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { queryPageApi, queryInfoApi, updateStrategyApi } from '@/api/strategy'

// 状态列表数据
const states = ref([{ name: '正常', value: 1 }, { name: '禁用', value: 0 }])
// 文章列表数据
const strategyList = ref([])
// 当前查看的文章
const currentPost = ref({})
// 详情对话框可见性
const detailVisible = ref(false)

// 搜索条件
const searchStrategy = ref({
  title: '',
  authorId: '',
  gameId: '',
  state: '0', // 默认查询未审核状态
  date: [],
  begin: '',
  end: ''
})

// 分页配置
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 声明token
const token = ref('')

onMounted(async () => {
  handleSearch()

  // 加载localStorage存储的用户登录信息
  const loginUser = localStorage.getItem('loginUser');
  if (loginUser) {
    token.value = JSON.parse(loginUser).token
  }
})

// 查询未审核文章
const handleSearch = async () => {
  console.log('Search:', searchStrategy.value)
  const result = await queryPageApi(
    searchStrategy.value.title || '', 
    searchStrategy.value.authorId || '', 
    searchStrategy.value.gameId || '',
    '0', // 固定查询未审核状态的文章
    searchStrategy.value.begin || '', 
    searchStrategy.value.end || '', 
    currentPage.value, 
    pageSize.value
  );
  if (result.code) {
    strategyList.value = result.data.records
    total.value = result.data.total
  }
}

// 重置搜索条件
const handleReset = () => {
  // 清空表单
  searchStrategy.value = {
    title: '',
    authorId: '',
    gameId: '',
    state: '0', // 保持默认查询未审核状态
    date: [],
    begin: '',
    end: ''
  }
  handleSearch()
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

// 查看文章详情
const handleView = async (id) => {
  console.log('View:', id)
  const result = await queryInfoApi(id);
  if (result.code) {
    currentPost.value = result.data
    detailVisible.value = true
  }
}

// 通过审核
const handleApprove = async (id) => {
  ElMessageBox.confirm('确定通过该文章的审核吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // 更新文章状态为已审核（状态值为1）
    const result = await updateStrategyApi({
      id: id,
      state: 1
    });
    if (result.code) {
      ElMessage.success('审核通过成功')
      detailVisible.value = false // 审核成功后关闭详情对话框
      handleSearch() // 重新查询未审核文章列表
    } else {
      ElMessage.error(result.msg)
    }
  })
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  return new Date(time).toLocaleDateString('zh-CN')
}
</script>

<style scoped>
.post-detail {
  max-width: 100%;
  padding: 20px;
}

.post-header {
  margin-bottom: 30px;
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
}

.post-header h1 {
  font-size: 24px;
  margin-bottom: 15px;
  color: #333;
}

.post-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.post-meta span {
  color: #666;
  font-size: 14px;
}

.post-content {
  line-height: 1.8;
  margin-bottom: 30px;
}

.markdown-body {
  font-size: 16px;
  color: #333;
  white-space: pre-wrap;
  word-break: break-word;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
  margin: 30px 0 15px 0;
  color: #333;
}

.markdown-body p {
  margin-bottom: 15px;
}
</style>